package org.example.GirdFind;

import org.example.bean.Cord;

import javax.swing.*;
import java.awt.*;
import java.util.List;

/**
 * @Desc:
 * @Author: yanghongjun
 * @Date: 2024/4/30
 **/
public class GirdMapDrawing extends JFrame {
    private static final int CELL_SIZE = 2; // 单元格大小
    private GameGirdMap map;
    private List<Cord> nodes;
    private List<Cord> nodes2;
    private int width, height;

    public GirdMapDrawing(GameGirdMap map, List<Cord> list, String name) {
        this.map = map;
        this.nodes = list;
        this.height = map.getWeight();
        this.width = map.getHeight();
        setTitle(name);
        setSize(width * CELL_SIZE, height * CELL_SIZE);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null); // 居中显示窗口
        // 添加一个自定义的面板用于绘图
        add(new DrawingPanel());
        // 添加一个带滚动条的面板用于绘图
        JScrollPane scrollPane = new JScrollPane(new DrawingPanel());
        add(scrollPane); // 将滚动面板添加到窗口中
    }
    public GirdMapDrawing(GameGirdMap map, List<Cord> list, List<Cord> list2,String name) {
        this.map = map;
        this.nodes = list;
        nodes2 = list2;
        this.height = map.getWeight();
        this.width = map.getHeight();
        setTitle(name);
        setSize(width * CELL_SIZE, height * CELL_SIZE);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null); // 居中显示窗口
        // 添加一个自定义的面板用于绘图
        add(new DrawingPanel());
        // 添加一个带滚动条的面板用于绘图
        JScrollPane scrollPane = new JScrollPane(new DrawingPanel());
        add(scrollPane); // 将滚动面板添加到窗口中
    }

    private class DrawingPanel extends JPanel {
        // 重写getPreferredSize方法以返回地图的实际大小
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(width * CELL_SIZE, height * CELL_SIZE);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;

            // 绘制网格背景
            for (int y = 0; y <= height; y++) {
                g2d.drawLine(0, y * CELL_SIZE, width * CELL_SIZE, y * CELL_SIZE);
            }
            for (int x = 0; x <= width; x++) {
                g2d.drawLine(x * CELL_SIZE, 0, x * CELL_SIZE, height * CELL_SIZE);
            }


            // 根据地图数据绘制单元格
            for (int y = 0; y < height; y++) {
                for (int x = 0; x < width; x++) {
                    if (map.getGirds()[x][y].isStopBlock()) { // 如果是障碍物
                        g2d.setColor(Color.BLACK);
                        g2d.fillRect(x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE);
                    }
                }
            }

            //绘制路径
            if (nodes2  != null){
                for (Cord cord : nodes2) {
                    g2d.setColor(Color.YELLOW);
                    g2d.fillRect(cord.getX() * CELL_SIZE, cord.getY() * CELL_SIZE, CELL_SIZE, CELL_SIZE);
                }
            }
            // 绘制路径
            g2d.setColor(Color.RED); // 设置线条颜色为红色
            if (!nodes.isEmpty()) {
                // 画出路径中的第一条线，从起点到第一个点
                Cord prevCord = nodes.get(0);
                g2d.drawLine(prevCord.getX() * CELL_SIZE + CELL_SIZE / 2, prevCord.getY() * CELL_SIZE + CELL_SIZE / 2,
                        prevCord.getX() * CELL_SIZE + CELL_SIZE / 2, prevCord.getY() * CELL_SIZE + CELL_SIZE / 2);

                // 遍历路径中的点，并画出连接它们的线
                for (int i = 1; i < nodes.size(); i++) {
                    Cord currentCord = nodes.get(i);
                    g2d.drawLine(prevCord.getX() * CELL_SIZE + CELL_SIZE / 2, prevCord.getY() * CELL_SIZE + CELL_SIZE / 2,
                            currentCord.getX() * CELL_SIZE + CELL_SIZE / 2, currentCord.getY() * CELL_SIZE + CELL_SIZE / 2);
                    prevCord = currentCord; // 更新前一个点为当前点，以便画下一条线
                }
            }
        }
    }
}
